package com.pet.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.mapper.DiaryMapper;
import com.pet.pojo.Diary;
import com.pet.pojo.ItemCat;
import com.pet.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class DiaryServiceImpl implements DiaryService{
    @Autowired
    private DiaryMapper diaryMapper;

    @Override
    public PageResult getDiaryListAllPublic(PageResult pageResult) {
        int pageNum = pageResult.getPageNum();  //获取页面
        int pageSize = pageResult.getPageSize();//获取条件
        //参数1: page分页对象
        Page<Diary> page = new Page(pageNum,pageSize);
        //参数2: 分页的查询条件  username模糊查询
        //问题:  如果用户没有传递query  like关键字  拼接参数
        //动态拼接: 传参拼接like  condition:true 拼接like条件
        //         false 不拼接 like关键字
        QueryWrapper<Diary> queryWrapper = new QueryWrapper<>();
        //判断用户是否传参  如果传参 返回true   反之 返回false
//        boolean flag = StringUtils.hasLength(pageResult.getQuery());
//        queryWrapper.like(flag,"title",pageResult.getQuery());
        queryWrapper.eq("status", 1);
        //规则: page2个参数 根据分页查询返回 total/分页后的记录 4个参数
        page = diaryMapper.selectPage(page,queryWrapper);
        //根据分页对象获取记录
        List<Diary> diaryList = page.getRecords();
        System.out.println(diaryList);
        //根据分页对象获取总数
        long total = page.getTotal();
        System.out.println(total);
        //封装VO对象 返回数据
        pageResult.setTotal(total).setRows(diaryList);
        return pageResult;
    }

    @Override
    public Diary getDiaryById(Integer id) {
        Diary diary = diaryMapper.selectById(id);
        return diary;
    }

    @Override
    @Transactional
    public void updateStatus(Diary diary) {//id/status

        diaryMapper.updateById(diary);
    }
}
